package com.ruoyi.user.controller;

import cn.hutool.core.util.RandomUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.SysFile;
import com.ruoyi.user.domain.User;
import com.ruoyi.user.service.UserService;
import com.ruoyi.user.utils.MobileMessageUtil;
import com.ruoyi.user.vo.LoginInfoVo;
import com.ruoyi.user.vo.UpdAvatarVo;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    RemoteFileService remoteFileService;

    /**
     * 点击修改头像
     */
    @CrossOrigin(origins = "http://localhost:8081")
    @PostMapping("/updateAvatar")
    public AjaxResult updateAvatar(@ModelAttribute  UpdAvatarVo updAvatarVo) {
        Integer userId = updAvatarVo.getUserId();
        R<SysFile> upload = remoteFileService.upload(updAvatarVo.getFile());
        String url = upload.getData().getUrl();// 获取上传后的url
        //获取用户信息
        User user = userService.getById(userId);
        user.setUserImg(url);
        //存储头像到数据库
        userService.updateById(user);
        return AjaxResult.success("更改成功,正在审核中......");
    }

    /**
     * 找回密码
     */
    @CrossOrigin(origins = "http://localhost:8081")
    @PostMapping("/findPassword")
    public AjaxResult findPassword(@RequestBody User user) {

        return userService.findPassword(user);

    }

    /**
     * 注册接口
     */
    @CrossOrigin(origins = "http://localhost:8081")
    @PostMapping("/register")
    public AjaxResult register(@RequestBody User user) {
       return userService.register(user);
    }

    /**
     * 手机号密码登录
     */
    @CrossOrigin(origins = "http://localhost:8081")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginInfoVo loginInfoVo) {
     return userService.login(loginInfoVo);
    }

    /**
     * 获取手机号验证码
     */
    @CrossOrigin(origins = "http://localhost:8081")
    @GetMapping("/getPhoneCode/{userMobile}")
    public AjaxResult getPhoneCode(@PathVariable String userMobile) {
        // 判断手机号是否存在
        User user = userService.lambdaQuery().eq(User::getUserMobile, userMobile).one();
        if (user == null) {
            return AjaxResult.error("手机号不存在");
        }
        //生成验证码
        String code = RandomUtil.randomNumbers(6);
        //短信发送验证码
        if (!MobileMessageUtil.sendMobileMessage(userMobile, code)) {
            return AjaxResult.error("验证码发送失败");
        }
        redisTemplate.opsForValue().set("code:" + userMobile, code,5, TimeUnit.MINUTES);
        System.out.println("验证码：" + code);
        return AjaxResult.success("验证码发送成功");
    }
}
